package cn.xfdzx.android.apps.shop.presenter;

import cn.xfdzx.android.apps.shop.base.BasePresenter;
import cn.xfdzx.android.apps.shop.bean.BaseObjectBean;
import cn.xfdzx.android.apps.shop.bean.CategoryAllBeen;
import cn.xfdzx.android.apps.shop.bean.ListingToPayBeen;
import cn.xfdzx.android.apps.shop.bean.PurchaseSubmitBeen;
import cn.xfdzx.android.apps.shop.bean.PurchaseSubmitEditBeen;
import cn.xfdzx.android.apps.shop.contract.ListingToPayContract;
import cn.xfdzx.android.apps.shop.contract.OneCategoryContract;
import cn.xfdzx.android.apps.shop.model.ListingToPayModel;
import cn.xfdzx.android.apps.shop.model.OneCategoryModel;
import cn.xfdzx.android.apps.shop.net.RxScheduler;
import io.reactivex.functions.Consumer;

/**
 * @author
 * @date Description：
 */
public class ListingToPayPresenter extends BasePresenter<ListingToPayContract.View> implements ListingToPayContract.Presenter {

    private ListingToPayContract.Model model;

    public ListingToPayPresenter() {
        model = new ListingToPayModel();
    }


    @Override
    public void initData(String index_status) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.initData( index_status)
                .compose(RxScheduler.<ListingToPayBeen>Flo_io_main())
                .as(mView.<ListingToPayBeen>bindAutoDispose())
                .subscribe(new Consumer<ListingToPayBeen>() {
                    @Override
                    public void accept(ListingToPayBeen bean) throws Exception {
                        mView.onInitSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }
    @Override
    public void purchaseEdit(PurchaseSubmitEditBeen body,int position) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.purchaseEdit(body)
                .compose(RxScheduler.<PurchaseSubmitBeen>Flo_io_main())
                .as(mView.<PurchaseSubmitBeen>bindAutoDispose())
                .subscribe(new Consumer<PurchaseSubmitBeen>() {
                    @Override
                    public void accept(PurchaseSubmitBeen bean) throws Exception {
                        mView.onPurchaseEditSuccess(bean,position);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void purchaseDelete(String purchase_sn,int position) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.purchaseDelete(purchase_sn)
                .compose(RxScheduler.<BaseObjectBean>Flo_io_main())
                .as(mView.<BaseObjectBean>bindAutoDispose())
                .subscribe(new Consumer<BaseObjectBean>() {
                    @Override
                    public void accept(BaseObjectBean bean) throws Exception {
                        mView.onPurchaseDeleteSuccess(bean,position);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

}
